﻿using System;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmParamDetails : FrmSysBase
    {
        private readonly GridHelper<SysParam> helper;
        private string id;

        public FrmParamDetails(string p_id)
        {
            InitializeComponent();
            id = p_id;
        }

        public FrmParamDetails(GridHelper<SysParam> p_helper)
        {
            InitializeComponent();
            helper = p_helper;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, DetailsFormEscResultCancel);
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            BindModel();
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            if (helper != null)
            {
                id = helper.SelectedId;
                btnPrev.Enabled = !helper.IsFirstFocus;
                btnNext.Enabled = !helper.IsLastFocus;
            }
            var model = ParamService.Instance.SelectById(id);
            if (model == null) return;

            WinHelper.SetFormData(layoutPanel, model);

            var hmodel = HistoryService.Instance.SelectLastByPrimaryKey(id);
            if (hmodel!=null)
            {
                editOperateUserName.Text = hmodel.OperateUserName;
                editOperateDateTime.Text = DateTimeHelper.FormatDateHasSecond(hmodel.OperateDateTime);
            }

            recordHistoryGridControl1.PrimaryKey = id;
        }


        private void FrmParamDetails_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            helper.View.MovePrev();
            BindModel();
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            helper.View.MoveNext();
            BindModel();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            ResultCancel();
        }
    }
}
